﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommerceOne.Common;
using CommerceOne.Controllers;

namespace CommerceOne.Widgets.CheckoutStep
{
    public class CheckoutStepController : ControllerBase
    {
        public CheckoutStepViewModel GetViewModel(string pageType)
        {
            var configPath = @"~\Configurations\CheckoutStep.xml";
            configPath = this.HttpContextBase.Server.MapPath(configPath);

            var configuration = SerializeService.Instance.DeserializeObject<Configurations>(configPath);
            if (configuration == null) throw new Exception("Config file doesnot existed. Check file: " + configPath);

            var viewModel = new CheckoutStepViewModel {Steps = configuration.Steps.OrderBy(p=>p.Index).ToList()};

            var isMakeUrl = true;
            foreach (var step in viewModel.Steps.Where(step => isMakeUrl))
            {
                if (step.PageType.Equals(pageType))
                {
                    isMakeUrl = false;
                    step.IsCurrent = true;
                    continue;
                }

                step.Url = UrlBuilderService.Instance.GetUrlRoute(step.PageType);
            }
            return viewModel;
        }
    }
}